/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permuteUnique = function (nums) {
    nums.sort((a, b) => a - b)
    let path = []
    const res = []
    let flagArr = new Array(nums.length).fill(false)
    const dfs = function () {
        if (path.length === nums.length) {
            res.push(path.slice())
        }
        for (let i = 0; i < nums.length; i++) {
            if (!flagArr[i]) {
                path.push(nums[i])
                flagArr[i] = true
                dfs()
                path.pop()
                flagArr = false
            }
        }
    }
    dfs()
    return res
};